package org.jsola.contract.web;

import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.PersonSignatureAddDTO;
import org.jsola.contract.dto.PersonSignatureUpdateDTO;
import org.jsola.contract.query.PersonSignatureQuery;
import org.jsola.contract.service.IPersonSignatureService;
import org.jsola.contract.vo.PersonSignatureListVO;
import org.jsola.contract.vo.PersonSignatureVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 合同个人签名表
 *
 * @author JanusMix
 */
@RestController("contractPersonSignatureController")
@RequestMapping(ContractConstants.API_PATH + "/v1/peopleSignature")
public class PersonSignatureController implements IPersonSignatureControllerAPI {

    @Autowired
    private IPersonSignatureService personSignatureService;

    @Override
    @PostMapping
    public Result<PersonSignatureVO> save(@Valid @RequestBody PersonSignatureAddDTO personSignatureAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增合同个人签名表权限校验

        return Result.success(
                personSignatureService.save(personSignatureAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody PersonSignatureUpdateDTO personSignatureUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改合同个人签名表权限校验

        int count = personSignatureService.update(personSignatureUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{personSignatureId}")
    public Result<Integer> delete(@PathVariable Long personSignatureId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除合同个人签名表权限校验

        int count = personSignatureService.deleteByIds(tokenUser, personSignatureId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] personSignatureIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除合同个人签名表权限校验

        int count = personSignatureService.deleteByIds(tokenUser, personSignatureIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{personSignatureId}")
    public Result<PersonSignatureVO> findById(@PathVariable Long personSignatureId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同个人签名表权限校验

        return Result.success(
            personSignatureService.selectById(personSignatureId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<PersonSignatureListVO>> listAll(@Valid PersonSignatureQuery personSignatureQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看合同个人签名表权限校验

        return Result.success(
            personSignatureService.select(personSignatureQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<PersonSignatureListVO>> page(@Valid PersonSignatureQuery personSignatureQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            personSignatureService.selectPage(personSignatureQuery, tokenUser.getSiteId())
        );
    }


}

